#!/usr/bin/ruby

require "sqlite3"
require 'pp'
# shared.rb is already imported by index.html which imports this

TIMESTAMP_FORMAT = "%A, %B %d, %H:%M"

def time2text(t)
    return t.strftime(TIMESTAMP_FORMAT)
end

def mk_timestamp(t_int, fname)
    `echo #{time2text(Time.at(t_int))} \
            | text2wave | sox - -c 1 -r #{RATE} #{fname}`
end

def join_recs(entries)
    #f = File.new("/home/yovadia/scanner-recorder/test.ogg", 'r')
    #$stdout.write(f.read())
    #return
    
    temp_dir = `mktemp -d`[0..-2]
    
    files = []
    entries.each do |start_t_int, fname_suf|
        rec_fname = RECS_DIR + fname_suf
        
        ts_fname = "#{temp_dir}/#{start_t_int}.wav"
        mk_timestamp(start_t_int, ts_fname)

        files << ts_fname; files << rec_fname
    end
    
    `sox #{files.join(' ')} #{outfile}`

    outfile = "#{temp_dir}/join.#{AUD_FORMAT}"
    f = File.new(outfile, 'r')
    puts "Content-type: application/ogg\n\n"
    $stdout.write(f.read())
    f.close()

    `rm #{outfile}`
    time_audio_l.each {|f| `rm #{f}`}
    `rmdir  #{temp_dir}`
end

